﻿using DoNet.Common.Cache;
using DoNet.Token.Entitys;

namespace DoNet.Token
{
    /// <summary>
    /// 应用数据
    /// 可以改成数据库操作
    /// </summary>
    public class AppService
    {
        /// <summary>
        /// 
        /// </summary>
        private static string keyName = "AppList";
        /// <summary>
        /// App缓存列表
        /// </summary>
        private static ListCacheManager<string, App> _cache = new ListCacheManager<string, App>();

        /// <summary>
        /// 实现数据库存储逻辑
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="accessToken"></param>
        /// <param name="refreshToken"></param>
        public static int StoreApp(string appId, string secret)
        {
            var model = new App()
            {
                AppId = appId,
                AppSecret = secret
            };

            //实现数据库
            _cache.AppendToList(keyName, model);
            return 0;
        }

        /// <summary>
        /// 通过AppId 获取 App
        /// </summary>
        /// <param name="refreshToken"></param>
        /// <returns></returns>
        public static App? GetAppByAppId(string appId)
        {
            //实现数据库
            var list = _cache.GetListCopy(keyName);
            if (list.Count > 0)
            {
                var model = list.Where(x => x.AppId == appId).FirstOrDefault();
                if (model != null)
                    return model;
            }
            return default;
        }
    }
}
